package fr.mywiki.view.web.struts.form.structure;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;

import fr.mywiki.business.structure.StructureLite;
import fr.mywiki.business.structure.TreeManager;
import fr.mywiki.model.ejbAdapter.DataController;

public class SelectMultiStructController extends SelectStructureController{
	/** The type of this controller : multiple */
	public static final String CONTROLLER_TYPE = "multiple";
	
	/** Id of the structure the controller is focused on */
	private Long idFocus;
	
	/** The structure the controller is focused on. */
	private StructureLite focusStruct;
	
	/**
	 * The selected structures. A <code>Set</code> of <code>StructureLite</code>
	 * objects.
	 */
	private Set selectedStructures;

	public Long getIdFocus() {
		return idFocus;
	}

	public void setIdFocus(Long idStructure) throws RemoteException {
		this.idFocus = idStructure;
		if (idStructure == null)
			setFocusStruct(null);
		else
			setFocusStruct((StructureLite) DataController.getValue(idStructure,
					StructureLite.TYPE_ENT));
	}
	
	public void centerTree(StructureLite structure) throws RemoteException, FinderException {
		if (structure == null)
			setIdFocus(null);
		else
			setIdFocus(structure.getId());

		setExpandJS(TreeManager.getExpandCodeJS(structure));
	}

	public StructureLite getFocusStruct() {
		return focusStruct;
	}

	public void setFocusStruct(StructureLite focusStruct) {
		this.focusStruct = focusStruct;
	}

	public Set getSelectedStructures() {
		return selectedStructures;
	}

	public void setSelectedStructures(Set selectedStructures) {
		this.selectedStructures = selectedStructures;
	}

	public String getControllerType() {
		return CONTROLLER_TYPE;
	}

	/**
	 * Creates a new controller, initialized with the collection of structures
	 * given in parameter and the request.
	 * 
	 * @param structures
	 *          a <code>Set</code> of <code>StructureLite</code>
	 *          objects.
	 * @param request
	 *          a <code>HttpServletRequest</code> object
	 * @return a <code>SelectMultiStructController</code> object.
	 */
	public static SelectMultiStructController createController(Collection structures,
			HttpServletRequest request) throws RemoteException, FinderException {
		SelectMultiStructController controller = new SelectMultiStructController();
		
		controller.setSelectedStructures(new HashSet(structures));
		controller.initTree(request);
		
		Iterator itStruct = structures.iterator();
		if(itStruct.hasNext())
			controller.setIdFocus(((StructureLite)itStruct.next()).getId());
		else controller.setIdFocus(null);
		
		return controller;
	}
	
	/**
	 * Loads the whole tree.
	 * 
	 * @param locale the current locale
	 * @return the html string description of the tree.
	 */
	public String loadTree(Locale locale) throws RemoteException, FinderException {
		Collection nodes = TreeManager.getTreeRootNodes();
		
		return TreeManager.buildTree(nodes,locale, new ArrayList(getSelectedStructures()));
	}
}
